package com.hhhn.wk.main.tab2.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hhhn.wk.R;
import com.hhhn.wk.utils.Constants;


import java.util.ArrayList;


public class ClassifyAdaper extends BaseAdapter {
    private static final String TAG = "ClassifyAdaper";
    // 定义Context
    private LayoutInflater mInflater;
    private ArrayList<TwoClass> list;
    private Context context;
    private TwoClass type;
    public ClassifyAdaper(Context context, ArrayList<TwoClass> list){
        mInflater= LayoutInflater.from(context);
        this.list=list;
        this.context=context;
    }

    @Override
    public int getCount() {
        if(list!=null&&list.size()>0)
            return list.size();
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyView view;
        if(convertView==null){
            view=new MyView();
            convertView=mInflater.inflate(R.layout.classify_adaper_item, null);

            view.name=(TextView)convertView.findViewById(R.id.typename);
            view.typeimg=(ImageView)convertView.findViewById(R.id.typeimg);
            convertView.setTag(view);
        }else{
            view=(MyView) convertView.getTag();
        }
        if(list!=null&&list.size()>0)
        {
            type=list.get(position);
            view.name.setText(type.getMsgtypeName());
            if (!type.getIconUrl().equals("")){
//                view.typeimg.setImageURI(Uri.parse(Constants.imgURL+type.getIconUrl()));
                Glide.with(context)
                        .load(Constants.HTTPIMG+type.getIconUrl())
                        .error(R.mipmap.img_class)//图片加载错误显示
                        .into(view.typeimg);
                Log.d(TAG, "getView: "+type.getIconUrl());
                Log.d(TAG, "getView: "+Constants.HTTPIMG+type.getIconUrl());
            }

        }

        return convertView;
    }


    private class MyView{
        private ImageView icon;
        private TextView name;
        private ImageView typeimg;
    }

}
